/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.dog.service.impl;

import com.dog.dao.CustomerDAO;
import com.dog.model.ContactPerson;
import com.dog.model.Customer;
import com.dog.service.IContactPersonService;
import com.dog.service.ICustomerService;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Kuttubek Aidaraliev
 * Date: 02.04.2010
 * Time: 08:52:04
 */
//@Transactional(rollbackFor = {NullPointerException.class})

@Service
public class CustomerService extends BaseService<CustomerDAO, Customer> implements ICustomerService {

    @Autowired
    private CustomerDAO customerDAO;

    @Autowired
    private IContactPersonService contactPersonService;


    @Override
    public CustomerDAO getDao() {
        return customerDAO;
    }

    @Override
    public Customer get2(final Long id){
        final Customer customer= super.get(id);
        if(null!=customer){
            Hibernate.initialize(customer.getContactPersons());
        }

        return customer;
    }

    @Override
    public Customer getByIdWithContactPersons(Long customerId) {
        return getDao().getByIdWithContactPersons(customerId);
    }

    @Override
    public void addContactPerson(Long customerId, ContactPerson contactPerson) {
        Customer customer = get(customerId);
        customer.getContactPersons().add(contactPerson);
        contactPerson.setCustomer(customer);
    }

    @Override
    public void deleteContactPerson(Long customerId, Long contractPersonId) {
        Customer customer = get(customerId);
        ContactPerson contactPerson = contactPersonService.get(contractPersonId);
        customer.getContactPersons().remove(contactPerson);
    }

}
